package com.cunfang.base.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.cunfang.entity.PageData;



@Repository("daoSupport")
public class DaoSupport implements DAO {

	
	@Resource(name = "sqlSessionTemplate")
	private SqlSessionTemplate sqlSessionTemplate;

	/**
	 * 保存对象
	 * 
	 * @param str
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object save(String str, Object obj) throws Exception {
		return sqlSessionTemplate.insert(str, obj);
	}

	/**
	 * 修改对象
	 * 
	 * @param str
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object update(String str, Object obj) throws Exception {
		return sqlSessionTemplate.update(str, obj);
	}

	/**
	 * 删除对象
	 * 
	 * @param str
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object delete(String str, Object obj) throws Exception {
		return sqlSessionTemplate.delete(str, obj);
	}

	/**
	 * 查找对象
	 * 
	 * @param str
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object findForObject(String str, Object obj) throws Exception {
		return sqlSessionTemplate.selectOne(str, obj);
	}

	/**
	 * 查找对象
	 * 
	 * @param str
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object findForList(String str, Object obj) throws Exception {
		return sqlSessionTemplate.selectList(str, obj);
	}

	public Object findForMap(String str, Object obj, String key, String value)
			throws Exception {
		return sqlSessionTemplate.selectMap(str, obj, key);
	}

	/** 判断是否存在 **/
	public Boolean findExist(String sql) {
		Connection conn = null;
		Statement stm = null;
		ResultSet rs = null;
		Boolean obj = false;
		SqlSession sqlSession = null;
		try {
			sqlSession = sqlSessionTemplate.getSqlSessionFactory()
					.openSession();
			conn = sqlSession.getConnection();
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
			obj = rs.next();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {

			try {
				if (rs != null)
					rs.close();
				if (stm != null)
					stm.close();
				if (sqlSession != null)
					sqlSession.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}

		}
		return obj;
	}

	/** sql获得单个值 **/
	public Object findSinge(String sql) {
		Connection conn = null;
		Statement stm = null;
		ResultSet rs = null;
		Object obj = "";
		SqlSession sqlSession = null;
		try {
			sqlSession = sqlSessionTemplate.getSqlSessionFactory()
					.openSession();
			conn = sqlSession.getConnection();
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
			if (rs.next()) {
				obj = rs.getObject(1);
			} else {
				obj = "";
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {

			try {
				if (rs != null)
					rs.close();
				if (stm != null)
					stm.close();
				if (sqlSession != null)
					sqlSession.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		return obj;
	}

	/** 执行sql **/
	public Integer ExecuteSql(String sql) {
		Connection conn = null;
		Statement stm = null;
		Integer rs = 0;
		SqlSession sqlSession = null;
		try {
			sqlSession = sqlSessionTemplate.getSqlSessionFactory()
					.openSession();
			conn = sqlSession.getConnection();
			stm = conn.createStatement();
			rs = stm.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			try {
				if (stm != null)
					stm.close();
				if (sqlSession != null)
					sqlSession.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		return rs;
	}

	/** 批量执行sql 实现事物 **/
	public Integer ExecuteSql(List<String> listsql) {
		Connection conn = null;
		Statement stm = null;
		Integer rs = 0;
		SqlSession sqlSession = null;
		try {
			sqlSession = sqlSessionTemplate.getSqlSessionFactory()
					.openSession();
			conn = sqlSession.getConnection();
			conn.setAutoCommit(false);
			stm = conn.createStatement();
			for (String sql : listsql) {
				stm.addBatch(sql);
				rs++;
			}
			stm.executeBatch();
			conn.commit();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			try {
				if (stm != null)
					stm.close();
				if (sqlSession != null)
					sqlSession.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		return rs;
	}

	/** 根据sql 获得list **/
	public List<PageData> Query(String sql) {
		Connection conn = null;
		Statement stm = null;
		ResultSet rs = null;
		List<PageData> list = new ArrayList<PageData>();
		SqlSession sqlSession = null;
		try {
			sqlSession = sqlSessionTemplate.getSqlSessionFactory()
					.openSession();
			conn = sqlSession.getConnection();
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
			PageData pd;
			while (rs.next()) {
				/* 逐行读取记录 */
				pd = new PageData();
				for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
					pd.put(rs.getMetaData().getColumnLabel(i),
							rs.getObject(rs.getMetaData().getColumnLabel(i)));
				}
				list.add(pd);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (stm != null)
					stm.close();
				if (sqlSession != null)
					sqlSession.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		return list;
	}

	

	/** 从连接池中取得一个JDBC连接 */
	public Connection getConnection() {
		return sqlSessionTemplate.getSqlSessionFactory().openSession()
				.getConnection();
	}

	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

}
